﻿$(function () {
    $("#menu").menu();
    $("#tabs").tabs();
    $("#div-logout").hide();
    $("#add-friend").hide();
});


setInterval(function () {
    $.getJSON("http://localhost:2425/api/chat?msg=" + $("#account_user").text(), null, function (data) {
        //alert(data);
        if (data != null) {
            if (document.getElementById(data.TaiKhoanGui)) {
                $('#' + data.TaiKhoanGui).append('<p>' + data.TaiKhoanGui + ': ' + data.NoiDung + '</p>');
            }
            else {
                $("#tab-title").append('<li><a href="#' + data.TaiKhoanGui + '">' + data.TaiKhoanGui + '</a><span class="ui-icon ui-icon-close">Remove Tab</span></li>');
                $("#tabs").append('<div class="chat-window" id="' + data.TaiKhoanGui + '"></div>');
                $("#tabs").tabs("refresh");
                $('#' + data.TaiKhoanGui).append('<p>' + data.TaiKhoanGui + ': ' + data.NoiDung + '</p>');
            }
            $(".chat-window").scrollTop($(".chat-window")[0].scrollHeight);
        }
    });
}, 1000);

setInterval(function () {
    var user = $("#account_user").text();
    if (user != "") {
        //alert($("#account_user").text());
        $.getJSON("http://localhost:2425/api/chat?acc=" + $("#account_user").text(), null, function (data) {
            if (data != null) {
                $("#menu").empty();
                data.forEach(function (value) {
                    if (value.TinhTrang == true) {
                        $("#menu").append('<li><a href="#' + value.TaiKhoan + '" onclick="return menu_click(&#39;' + value.TaiKhoan + '&#39;)"><img class="avatar" src="http://localhost:2425/hinhanh/' + value.TaiKhoan + '.jpg" onError="return ImgError(this)" />  ' + value.TaiKhoan + '</a></li>');
                        $("#menu").menu("refresh");
                    }
                    else {
                        $("#menu").append('<li><a class="offline" href="#' + value.TaiKhoan + '" onclick="return menu_click(&#39;' + value.TaiKhoan + '&#39;)"><img class="avatar" src="HinhAnh/Offline.jpg" />  ' + value.TaiKhoan + '</a></li>');
                        $("#menu").menu("refresh");
                    }
                });
            }
        });
    }
}, 5000);

function ImgError(source) {
    source.src = "Hinhanh/noimage.jpg";
    source.onerror = "";
    return true;
}

window.onbeforeunload = function (e) {

    var acc = {
        TaiKhoan: $("#account_user").text(),
        MatKhau: "",
        TinhTrang: true
    }

    $.post("http://localhost:2425/api/chat?logout=", acc, function (data) {
        if (data == 1) {
            $("#account_user").empty();
            $("#menu").empty();
            $("#menu").menu("refresh");
            $("#tab-title").empty();
            $("#tabs").children("div").remove();
        }
    }, "json");
};

// close icon: removing the tab on click
$("#tabs span.ui-icon-close").live("click", function () {
    var panelId = $(this).closest("li").remove().attr("aria-controls");
    $("#" + panelId).remove();
    tabs.tabs("refresh");
});

function Button_login_onclick() {
    var userText = $("#TextBox_UserName").val();
    var passText = $("#TextBox_Passwork").val();
    $.getJSON("http://localhost:2425/api/chat?acc=" + userText + "&pass=" + passText, null, function (data) {
        if (data != null) {
            $("#account_user").text($("#TextBox_UserName").val());

            data.forEach(function (value) {
                if (value.TinhTrang == true) {
                    $("#menu").append('<li><a href="#' + value.TaiKhoan + '" onclick="return menu_click(&#39;' + value.TaiKhoan + '&#39;)"><img class="avatar" src="http://localhost:2425/hinhanh/' + value.TaiKhoan + '.jpg" onError="return ImgError(this)" />  ' + value.TaiKhoan + '</a></li>');
                    $("#menu").menu("refresh");
                }
                else {
                    $("#menu").append('<li><a class="offline" href="#' + value.TaiKhoan + '" onclick="return menu_click(&#39;' + value.TaiKhoan + '&#39;)"><img class="avatar" src="HinhAnh/Offline.jpg" />  ' + value.TaiKhoan + '</a></li>');
                    $("#menu").menu("refresh");
                }
            });
            $("#div-logout").show();
            $("#div-login").hide();
            $("#add-friend").show();
            $("#hinh-dai-dien").append('<img class="useravatar" src="http://localhost:2425/hinhanh/' + userText + '.jpg" onError="return ImgError(this)" />');
        }
        else {
            alert("Tài khoản hoặc mật khẩu không chính xác");
        }
    });

}

function menu_click(data) {
    var idchecker = document.getElementById(data);
    if (idchecker == null) {
        $("#tab-title").append('<li><a href="#' + data + '">' + data + '</a><span class="ui-icon ui-icon-close">Remove Tab</span></li>');
        $("#tabs").append('<div class="chat-window" id="' + data + '"></div>');
        $("#tabs").tabs("refresh");
    }
}

function Button_send_onclick() {
    var id = $(".ui-state-active").children().first().text();
    var text = $("#textbox_send").val();
    if (text != "")
        $('#' + id).append('<p>' + $("#account_user").text() + ': ' + text + '</p>');
    var message = {
        TaiKhoanGui: $("#account_user").text(),
        TaiKhoanNhan: id,
        NoiDung: text
    }
    $.post("http://localhost:2425/api/chat?SendMsg=", message, function (data) {
        if (data == -1)
            alert("Có lỗi");
    }, "json");
    $(".chat-window").scrollTop($(".chat-window")[0].scrollHeight);
    $("#textbox_send").val("");
}

function Button_AddFriend_onclick() {
    var myFriend = {
        TaiKhoan: $("#account_user").text(),
        TaiKhoanBan: $("#text_AddFriend").val()
    }
    //            $("#menu").empty();
    //            $.ajax({
    //                url: "http://localhost:2425/api/chat",
    //                data: 'aaaa',
    //                contentType: 'application/json',
    //                type: 'POST',
    //                dataType: 'json',
    //                success: function (mydata) {
    //                    alert(mydata);
    //                    if (mydata != null) {
    //                        $("#menu").empty();
    //                        mydata.forEach(function (value) {
    //                            $("#menu").append('<li><a href="#' + value.TaiKhoan + '" onclick="return menu_click(&#39;' + value.TaiKhoan + '&#39;)">' + value.TaiKhoan + '</a></li>');
    //                            $("#menu").menu("refresh");
    //                        });
    //                    }
    //                },
    //                failure: function(errMsg) {
    //                    alert(errMsg);
    //                }
    //            });
    $.post("http://localhost:2425/api/chat", myFriend, function (mydata) {
        //                alert(mydata);
        if (mydata != null) {
            $("#menu").empty();
            mydata.forEach(function (value) {
                if (value.TinhTrang == true) {
                    $("#menu").append('<li><a href="#' + value.TaiKhoan + '" onclick="return menu_click(&#39;' + value.TaiKhoan + '&#39;)"><img class="avatar" src="http://localhost:2425/hinhanh/' + value.TaiKhoan + '.jpg" onError="return ImgError(this)" />  ' + value.TaiKhoan + '</a></li>');
                    $("#menu").menu("refresh");
                }
                else {
                    $("#menu").append('<li><a class="offline" href="#' + value.TaiKhoan + '" onclick="return menu_click(&#39;' + value.TaiKhoan + '&#39;)"><img class="avatar" src="HinhAnh/Offline.jpg" />  ' + value.TaiKhoan + '</a></li>');
                    $("#menu").menu("refresh");
                }
            });
        }
    }, "json");
}

function Button_logout_onclick() {
    var acc = {
        TaiKhoan: $("#account_user").text(),
        MatKhau: "",
        TinhTrang: true
    }

    $.post("http://localhost:2425/api/chat?logout=", acc, function (data) {
        if (data == 1) {
            $("#account_user").empty();
            $("#menu").empty();
            $("#menu").menu("refresh");
            $("#tab-title").empty();
            $("#tabs").children("div").remove();
        }
    }, "json");

    $("#div-logout").hide();
    $("#div-login").show();
    $("#add-friend").hide();
    $("#hinh-dai-dien").empty();
}

function Button_deleteFriend_onclick() {
    var id = $(".ui-state-active").children().first().text();
    var myFriend = {
        TaiKhoan: $("#account_user").text(),
        TaiKhoanBan: id
    }

    $.post("http://localhost:2425/api/chat?deleteFriend=", myFriend, function (data) {
        if (data != null) {
            $("#menu").empty();
            data.forEach(function (value) {
                if (value.TinhTrang == true) {
                    $("#menu").append('<li><a href="#' + value.TaiKhoan + '" onclick="return menu_click(&#39;' + value.TaiKhoan + '&#39;)"><img class="avatar" src="http://localhost:2425/hinhanh/' + value.TaiKhoan + '.jpg" onError="return ImgError(this)" />  ' + value.TaiKhoan + '</a></li>');
                    $("#menu").menu("refresh");
                }
                else {
                    $("#menu").append('<li><a class="offline" href="#' + value.TaiKhoan + '" onclick="return menu_click(&#39;' + value.TaiKhoan + '&#39;)"><img class="avatar" src="HinhAnh/Offline.jpg" />  ' + value.TaiKhoan + '</a></li>');
                    $("#menu").menu("refresh");
                }
            });
        }
    }, "json");
}